﻿
using Catalog.API.Products.CreateProduct;

namespace Catalog.API.Products.GetProducts
{
    //由于该请求没有参数，因此不需要定义请求参数类型
    public record GetProductsRequest(int PageNumber, int PageSize);
    public record GetProductsResponse(IEnumerable<Product> Products);
    public class GetProductsEndpoint : ICarterModule
    {
        public void AddRoutes(IEndpointRouteBuilder app)
        {
            //查询参数的写法: https://localhost:5050/products?pageNumber=1&pageSize=5
            app.MapGet("/products", async ([AsParameters] GetProductsRequest request,ISender sender) =>
            {
                var query=request.Adapt<GetProductsQuery>();
                var result=await sender.Send(query);
                var response=result.Adapt<GetProductsResponse>();
                return Results.Ok(response);
            }
            ).WithName("GetProducts")
           .Produces<GetProductsResponse>(StatusCodes.Status200OK)
           .ProducesProblem(StatusCodes.Status400BadRequest)
           .WithSummary("Get  products")
           .WithDescription("Get  products");

        }
    }
}
